<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* 
*/
class Cart extends D_Controller
{
	
	public function __construct()
	{
		parent::__construct();
		
		$this->load->model('cart/m_cart');
	}
	
	public function index(){
	    if(!$this->session->userdata('customer_id')){
	        redirect('login');
	    }
	    
	    $data['carts'] = $this->m_cart->get_shopping_carts();
	    $this->load->view_tpls('v_cart', $data);
	}

	public function add(){
	    var_dump($_POST);
	    $this->load->library('form_validation');
	    $this->form_validation->set_rules('product_id','Product','required|is_natural_no_zero|is_exists[products.product_id]');
	    $this->form_validation->set_rules('quantity','Quantity','required|is_natural_no_zero');
	    
	    if ($this->form_validation->run() == FALSE){
	        redirect($this->input->server('HTTP_REFERER'));
	    }
	    
	    if(!$this->session->userdata('customer_id')){
	        redirect('login');
	    }
	    
	    $shopping_cart = $this->m_cart->is_in_cart($_POST);
	    
	    
	    if($shopping_cart == FALSE){
	        $shopping_cart_id = $this->m_cart->insert($_POST);
	    }else{
	        $_POST['quantity'] = $_POST['quantity'] + $shopping_cart['quantity'];
	        $shopping_cart_id = $shopping_cart['shopping_cart_id'];
	        $flag = $this->m_cart->update_quantity($shopping_cart_id, $_POST['quantity']);
	    }
	    
	    if($shopping_cart_id < 0){
	        echo 'add cart error...';
	        echo $shopping_cart_id;
	    }else{
	        echo 'add cart successful...';
	        redirect('cart');
	    }
	}
	
	public function delete($cid){
	    $this->m_cart->delete($cid);
	    redirect('cart');
	}
}

/* End of file cart.php */
/* Location: ./application/controllers/cart.php */